#pragma once
#include <iostream>
#include <string.h>
#include <stdarg.h> //可变参数库 c
#include <cstdio>
#include <ctime>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define BUFFERSIZE 1024
#define DEFPATH "./log/"
#define LOGFILE "log.txt"

enum LEVEL
{
    INFO = 0,
    DEBUG,
    WARNING,
    ERROR,
    FATAL,
};

enum PMETHOD
{
    SCREEN = 0, // 写入到屏幕
    ONEFILE,    // 写入到同一个文件
    CLASSFILE,  // 根据日志等级写入到不同的文件
};

int sum(int n, ...)
{
    va_list s;      // char*指针（记录可变参数）
    va_start(s, n); // 计算可变参数的开始位置
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += va_arg(s, int); // 解析可变参数
    }
    va_end(s); // s=NULL
    return sum;
}

class Log
{
public:
    Log()
    {
    }
    
    Log(PMETHOD pmethod)
    {
        _printMethod = pmethod;
    }

    Log(std::string path, PMETHOD pmethod)
    {
        _path = path;
        _printMethod = pmethod;
    }

    void PrintLog(LEVEL level, const std::string& log)
    {
        switch (_printMethod)
        {
        case SCREEN:
            std::cout << log;
            break;
        case ONEFILE:
            oneFilePrint(LOGFILE,log);
            break;
        case CLASSFILE:
            classFilePrint(level,log);
            break;
        default:
            break;
        }
    }
    
    void oneFilePrint(const std::string& filename, const std::string& log)
    {
        std::string _filename = _path + filename;
        // std::cout<<_filename<<std::endl; 测试代码
        int fd = open(_filename.c_str(),O_CREAT|O_APPEND|O_WRONLY,0666);
        if(fd < 0)
        {
            perror("open");
            exit(1);
        }
        write(fd,log.c_str(),log.size());
        close(fd);
    }

    void classFilePrint(LEVEL level,const std::string& log)
    {
        std::string filename = LOGFILE;
        filename += "." ;
        filename += levelToString(level);
        oneFilePrint(filename,log);
    }

    std::string levelToString(LEVEL level)
    {
        switch (level)
        {
        case INFO:
            return "INFO";
        case DEBUG:
            return "DEBUG";
        case WARNING:
            return "WARNING";
        case ERROR:
            return "ERROR";
        case FATAL:
            return "FATAL";
        default:
            return nullptr;
        }
    }

    /*
     * 第一个参数是错误等级
     * 第二个参数是格式化形式
     * 可变参数是错误信息和错误码
     */
    void operator()(LEVEL level, const char *format, ...)
    {
        time_t t = time(nullptr);
        struct tm *ctime = localtime(&t);
        char leftBuffer[BUFFERSIZE]; // 日志等级和时间
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%d-%d-%d %d:%d:%d]",
                 levelToString(level).c_str(), ctime->tm_year + 1900, ctime->tm_mon + 1, ctime->tm_mday,
                 ctime->tm_hour, ctime->tm_min, ctime->tm_sec);
        // std::cout<<leftBuffer<<std::endl; 测试代码

        char rightBuffer[BUFFERSIZE]; // 错误信息和错误码
        va_list s;                    // 一个记录可变参数的变量
        va_start(s, format);
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, s);
        va_end(s);

        char logtxt[BUFFERSIZE * 2];
        snprintf(logtxt, sizeof(logtxt), "%s:%s",leftBuffer, rightBuffer);
        // std::cout<<logtxt<<std::endl; 测试代码

        std::string log(logtxt);
        log +="\n";
        PrintLog(level,log);
    }

private:
    std::string _path = "./"; 
    PMETHOD _printMethod = ONEFILE; // 输出方式
};

Log lg;